package com.shzq.test;

import com.shzq.MainRunnerApplication;
import com.shzq.dao.CountMapper;
import com.shzq.dao.EarthquakeMapper;
import com.shzq.service.ArithmeticService;
import com.shzq.service.RedisService;
import com.shzq.service.ReportService;
import com.shzq.util.POIUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.*;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@SpringBootTest(classes = MainRunnerApplication.class)
@RunWith(SpringRunner.class)
@Slf4j
public class Tool {
    @Autowired
    private EarthquakeMapper earthquakeMapper;
    private CountMapper countMapper;


    @Autowired
    private ReportService reportService;
    @Test
    public void show() throws IOException {
        String fileName = "F:\\java\\ Fwork\\hybb\\src\\main\\resources\\data.xlsx";
        List<String[]> excel = POIUtils.readExcel(fileName);
        for (String[] ss : excel) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("latitude",ss[2]+":"+ss[3]);
            map.put("occurrenceTime",new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
            map.put("depth",ss[4]);
            map.put("strength",ss[5]);
//            System.out.println(ss[6]);
            String decode = URLDecoder.decode(ss[6], "utf-8");
//            System.out.println(decode);
            map.put("address", decode);
            earthquakeMapper.add(map);
        }
    }
    @Test
    public void show2() throws IOException {
        File file = new File("F:\\java\\ Fwork\\hybb\\src\\main\\resources\\date.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        int i = 1;
        while (true){
            String s = br.readLine();
            if(s!=null){
                HashMap<String,Object> map = new HashMap<>();
                map.put("id",i);
                map.put("date",s);
                earthquakeMapper.update(map);
//                System.out.println(map);
                i++;
            }
            else break;
        }
    }

    @Test
    public void show3(){

    }

    @Autowired
    private RedisService redisService;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Test
    public void show4(){
        long begin = System.currentTimeMillis();
        redisService.importData();
        System.out.println(System.currentTimeMillis() - begin);
    }


    @Test
    public void show5(){
        List<Point> points = redisTemplate.boundGeoOps("latitude").position("id_80");
        Circle circle = new Circle(points.get(0),100000);
        List<GeoResult<RedisGeoCommands.GeoLocation<String>>> latitude = redisTemplate.boundGeoOps("latitude").radius(circle).getContent();
        System.out.println(latitude);
    }

    @Test
    public void show6(){
        List<Integer> ids = earthquakeMapper.findAllIds();
        //遍历每个点
        List<String> collect = ids.stream().map(id -> "id_" + id).collect(Collectors.toList());
        String[]idArr = new String[collect.size()];
        collect.toArray(idArr);
        //把id转换成点集
        List<Point> latitude = redisTemplate.boundGeoOps("latitude").position(idArr);
    }

    @Autowired
    private ArithmeticService arithmeticService;
    @Test
    public void show7(){
        System.out.println(arithmeticService.createData());
    }

    @Test
    public void show8(){
        System.out.println(arithmeticService.createData());
    }
}
